#region License, Terms and Conditions
//
// Prawks - Copyright (c) 2007 Atif Aziz. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
#endregion

namespace Prawks.CodeProvisioningCompiler
{
    using System;
    using System.Diagnostics;
    
    using AssemblyCopyrightAttribute = System.Reflection.AssemblyCopyrightAttribute;
    using AssemblyTitleAttribute = System.Reflection.AssemblyTitleAttribute;
    using Assembly = System.Reflection.Assembly;

    sealed class Application
    {
        private static readonly TraceSwitch _generalTracing = new TraceSwitch("General", "General tracing");

        private Application() {}

        static int Main(string[] args)
        {
            try
            {
                WriteLogo();
                Console.WriteLine();

                //
                // If no arguments were specified then display usage.
                //

                if (args.Length == 0)
                {
                    CompilerOptions.ShowCommandLineUsage();
                    return 0;
                }

                //
                // Parse the command-line arguments.
                //

                CompilerOptions options = CompilerOptions.ParseFromArgs(args);
                Debug.Assert(options.BaseAssemblyPath != null);

                if (options.VerboseReporting)
                {
                    _generalTracing.Level = TraceLevel.Verbose;
                    Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
                }

                Compiler.Compile(options);
            
                return 0;
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);

                if (_generalTracing.TraceVerbose)
                    Trace.WriteLine(e);

                return -1;
            }
        }

        private static void WriteLogo()
        {
            Assembly thisAssembly = typeof(Compiler).Assembly;
            
            AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute) Attribute.GetCustomAttribute(thisAssembly, typeof(AssemblyTitleAttribute));
            string title = titleAttribute == null ? 
                thisAssembly.GetName().Name : titleAttribute.Title;

            AssemblyCopyrightAttribute copyrightAttribute = (AssemblyCopyrightAttribute) Attribute.GetCustomAttribute(thisAssembly, typeof(AssemblyCopyrightAttribute));
            string copyright = copyrightAttribute == null ? 
                "[Copyright Notice]" : copyrightAttribute.Copyright;

            Console.Out.WriteLine("\n{0} V{1}", title, thisAssembly.GetName().Version);
            Console.Out.WriteLine(copyright);
        }
    }
}
